<!DOCTYPE html>
<html>
<head>
    <title>发药--中软高科-2015</title>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="../Css/bootstrap.css" />
    <link rel="stylesheet" type="text/css" href="../Css/bootstrap-responsive.css" />
    <link rel="stylesheet" type="text/css" href="../Css/style.css" />
    <script type="text/javascript" src="../Js/jquery.js"></script>
    <script type="text/javascript" src="../Js/jquery.sorted.js"></script>
    <script type="text/javascript" src="../Js/bootstrap.js"></script>
    <script type="text/javascript" src="../Js/ckform.js"></script>
    <script type="text/javascript" src="../Js/common.js"></script>

    <style type="text/css">
        body {
            padding-bottom: 40px;
        }
        .sidebar-nav {
            padding: 9px 0;
        }

        @media (max-width: 980px) {
            /* Enable use of floated navbar text */
            .navbar-text.pull-right {
                float: none;
                padding-left: 5px;
                padding-right: 5px;
            }
        }


    </style>
    <script type="text/javascript">
	 $(function () {
		$('#newNav').click(function(){
				window.location.href="dispensing-gives.html";
		 });
    });
	
    	function checkall(){
			var alls=document.getElementsByName("check");
			var ch=document.getElementById("checkall");
			if(ch.checked){
				for(var i=0;i<alls.length;i++){
					alls[i].checked=true;	
				}	
			}else{
				for(var i=0;i<alls.length;i++){
					alls[i].checked=false;	
				}	
			}
		}
		function delAll(){
			var alls=document.getElementsByName("check");
			var ids=new Array();
			for(var i=0;i<alls.length;i++){
				if(alls[i].checked){
					ids.push(alls[i].value);
				}		
			}
			if(ids.length>0){
				if(confirm("确认操作?")){
					alert("成功!");
				}
			}else{
				alert("请选中要操作的项");
			}
		}
    </script>
</head>
<body>

<form id="myForm" action="dispensing.html" method="post" class="definewidth m20">
<table class="table table-bordered table-hover definewidth m10">
    <tr>
        <td width="10%" class="tableleft">病例号：</td>
        <td><input type="text" name="hid" value=""/></td>
        <td width="10%" class="tableleft">页号：</td>
        <td><input  type="text" class="abc input-default"  placeholder="" name="pageNum" value="1" /> &nbsp;</td>
        <td width="10%" class="tableleft">单页条数：</td>
        <td><select name="pageSize">
            <option value="5" selected="selected">5</option>
            <option value="10">10</option>
            <option value="15">15</option>
        </select></td>
    </tr>
    <tr>
		  <td colspan="2"><center>
            <button onclick="loadData()" class="btn btn-primary" type="button">查询</button>
            <button type="reset" class="btn btn-primary">清空</button>
			</center>
        </td>
    </tr>
</table>
</form>
<!--在线发药列表展示-->
<table class="table table-bordered table-hover definewidth m10" >
   <thead>
    <tr>
    	<th><input type="checkbox" id="checkall" onChange="checkall();"></th>
        <th>病历号</th>
        <th>姓名</th>
        <th>负责人</th>
        <th>操作</th>
    </tr>
    </thead>
    <tbody id="tbody">
        <tr>
            <td style="vertical-align:middle;"><input type="checkbox" name="check" value="1"></td>
            <td style="vertical-align:middle;">1103</td>
            <td style="vertical-align:middle;">黄飞鸿</td>
            <td style="vertical-align:middle;">胡一得</td>
            <td style="vertical-align:middle;"><a href="dispensing-give.html">发药</a>&nbsp;&nbsp;&nbsp;<a href="dispensing-look.html">详情...</a></td>
        </tr>
    </tbody>
  </table>

<!--分页部分-->
<table class="table table-bordered table-hover definewidth m10" >
    <tr><th colspan="5">  <div class="inline pull-right page" id="ppp">
        <span name="page1" id="firstPage"><a href="javascript:void(0)">第一页</a></span>
        <span name="page2" id="prePage"><a href="javascript:void(0)">上一页</a></span>
        <span name="page3" id="nextPage"><a href="javascript:void(0)">下一页</a></span>
        <span name="page4" id="lastPage"><a href="javascript:void(0)">最后一页</a></span>
        共<span  id="total"></span> 条记录 <span id="pageNum">1</span> /<span id="pages">1</span>页</div>
        <div>
            <button type="button" class="btn btn-success" onclick="giveDrugs()">发药</button>
        </div>

    </th></tr>
</table>

<script>
    String.prototype.GetValue= function(para) {
        let reg = new RegExp("(^|&)"+ para +"=([^&]*)(&|$)");
        let r = this.substr(this.indexOf("\?")+1).match(reg);
        if (r!=null) return unescape(r[2]); return null;
    }
    //页面加载完毕之后发送异步请求
    $(function(){
        loadData();
        $("#firstPage").click(function(){
            if(($(this).attr("class"))=='disabled')
                return;
            let num=$(this).attr("name");
            $("input[name='pageNum']").val(num);
            loadData();
        });
        $("#prePage").click(function(){
            if(($(this).attr("class"))=='disabled')
                return;
            let num=$(this).attr("name");
            $("input[name='pageNum']").val(num);
            loadData();
        });
        $("#nextPage").click(function(){
            if(($(this).attr("class"))=='disabled')
                return;
            let num=$(this).attr("name");
            $("input[name='pageNum']").val(num);
            loadData();
        });
        $("#lastPage").click(function(){
            if(($(this).attr("class"))=='disabled')
                return;
            let num=$(this).attr("name");
            $("input[name='pageNum']").val(num);
            loadData();
        });

    });
    //填充分页信息
    function fillPageData(pageInfo) {
        //填充信息
        $("#total").html(pageInfo.total);
        $("#pages").html(pageInfo.pages);
        $("span[id='pageNum']").val(pageInfo.pageNum);
        $("select[name='pageSize']").val(pageInfo.pageSize);
        $("input[name='pageNum']").val(pageInfo.pageNum);
        //设置按钮的属性值和样式
        $("#firstPage").attr("name",1);
        $("#prePage").attr("name",pageInfo.prePage);
        $("#nextPage").attr("name",pageInfo.nextPage);
        $("#lastPage").attr("name",pageInfo.pages);
        $("#firstPage").removeClass("disabled");
        $("#prePage").removeClass("disabled");
        $("#nextPage").removeClass("disabled");
        $("#lastPage").removeClass("disabled");
        if(pageInfo.isFirstPage){//如果是第一页：上一页和首页禁用
            $("#firstPage").addClass("disabled");
            $("#prePage").addClass("disabled");
        }
        if(pageInfo.isLastPage){//如果已经是最后一页：下一页和末页禁用
            $("#nextPage").addClass("disabled");
            $("#lastPage").addClass("disabled");
        }
    }
    //更新按钮
    function give(id,registerName) {
        registerName = encodeURI(registerName);
        window.location.href = "/hospital/dispensing-give.html?hid=" + id + "&registerName=" + registerName;
    }

    //查看详情按钮
    function message(id,registerName) {
        registerName = encodeURI(registerName);
        let pageNum = $("input[name='pageNum']").val();
        let pageSize = $("select[name='pageSize']").val();
        window.location.href = "/hospital/dispensing-look.html?hid=" + id+"&registerName="+registerName+ "&pageNum=" + pageNum + "&pageSize=" + pageSize;
    }

    //发药按钮
    function giveDrugs() {
        let alls = document.getElementsByName("check");
        let ids = new Array();
        let names = new Array();
        for (let i = 0; i < alls.length; i++) {
            if (alls[i].checked) {
                ids.push(alls[i].value.GetValue("hid"));
                // console.log(ids[i])
                names.push(alls[i].value.GetValue("registerName"));
                urlNames = encodeURI(names);
                // console.log(decodeURI(urlNames))
            }
        }
        if (ids.length > 0) {
            window.location.href = "/hospital/dispensing-gives.html?hids=" + ids + "&registerNames=" + urlNames;
        } else {
            alert("请选中要操作的项");
        }
    }

    //加载数据
    function loadData() {
        let url = document.location.toString();
        let pageNum = url.GetValue("pageNum");
        let pageSize = url.GetValue("pageSize");
        if (pageNum != null && $("input[name='pageNum']").val() != null) {
            $("input[name='pageNum']").val(pageNum);
        }
        if (pageSize != null && $("select[name='pageSize']").val() != null) {
            $("select[name='pageSize']").val(pageSize);
        }
        //alert($("#myForm").serialize())
        $.ajax({
            type: "GET",
            url: "/dispensing/list",
            data: $("#myForm").serialize(),//主要是分页和多条件参数数据
            dataType: "json",
            success: function (vo) {
                //alert( "Data Saved: " + vo );
                let list = vo.pageInfo.list;
                let str = "";
                for (let i = 0; i < list.length; i++) {
                    let obj = list[i];
                    let hid = obj.id;
                    let registerName = obj.register.registerName == null ? '' : obj.register.registerName;
                    let doctorName = obj.register.doctor.doctorName == null ? '' : obj.register.doctor.doctorName;
                    let btnStr = '<a  onclick="give(' + hid + ',\'' + registerName + '\')">发药&nbsp;&nbsp;' +
                        '<a onclick="message(' + hid +',\'' + registerName + '\')">详情</a>&nbsp;';

                    str += '<tr >' +
                        '            <td style="vertical-align:middle;"><input type="checkbox" name="check" value="hid=' + hid + '&registerName=' + registerName + '"/></td>' +
                        '            <td>' + hid + '</td>' +
                        '            <td>' + registerName + '</td>' +
                        '            <td>' + doctorName + '</td>' +
                        '            <td>' + btnStr + '</td>' +
                        '</tr>'
                }
                $("#tbody").html(str);
                //填充分页数据
                fillPageData(vo.pageInfo);
            }
        });
    }
</script>
</body>
</html>
